<?php
namespace App\Controllers\Message;

use App\Models\User\User;
use App\Libs\Views\Json;
use App\Libs\Base\Constant;
use App\Models\Tag\Tags;
use App\Models\Member\MemberLevel;
use App\Libs\Helper\CommonHelper;
use App\Models\Message\Message;
class GetMessageSummarize extends \App\Libs\Base\BaseAction
{

    public function execute()
    {
        $user = $this->session->user??[];
        if(empty($user)){
            return Json::view(Constant::UNLOGIN,'请登录',NULL);
        }
        $finalData = [];
        //推广消息
        $TGNum = Message::count([
            'uid=?1 and hread=0 and type=1',
            'bind'=>[1=>$user->id]
        ]);
        if(!empty($TGNum)){
            $newlestTG = Message::findOne([
                'uid=?1 and hread=0 and type=1',
                'bind' => [1=>$user->id],
                'order' => 'ctime desc'
            ]);
            $finalData[] = [
              'msg_title' => '推广消息',
              'unread_num' => $TGNum,
              'newlest_msg_title' => '【'.$newlestTG->title.'】',
              'newlest_msg_time' =>  User::secToTime(time()-strtotime($newlestTG->ctime)).'前',
            ];
        }else{
            $finalData[] = [
                'msg_title' => '推广消息',
                'unread_num' => 0,
                'newlest_msg_title' => '',
                'newlest_msg_time' =>  '',
            ];
        }
        //班级消息
        $msgBJ = Message::count([
            'uid=?1 and hread=0 and type=2',
            'bind'=>[1=>$user->id]
        ]);
        if(!empty($msgBJ)){
            $newBJMsg = Message::findOne([
                'uid=?1 and hread=0 and type=2',
                'bind' => [1=>$user->id],
                'order' => 'ctime desc'
            ]);
            $finalData[] = [
                'msg_title' => '班级消息',
                'unread_num' => $msgBJ,
                'newlest_msg_title' => '【'.$newBJMsg->title.'】',
                'newlest_msg_time' =>  User::secToTime(time()-strtotime($newlestTG->ctime)).'前'
            ];
        }else{
            $finalData[] = [
                'msg_title' => '班级消息',
                'unread_num' => 0,
                'newlest_msg_title' => '',
                'newlest_msg_time' =>  '',
            ];
        }
        //系统消息
        $sysMsgNum = Message::count([
            'uid=?1 and hread=0 and type=3',
            'bind'=>[1=>$user->id]
        ]);
        if(!empty($sysMsgNum)){
            $newSysMsg = Message::findOne([
                'uid=?1 and hread=0 and type=3',
                'bind' => [1=>$user->id],
                'order' => 'ctime desc'
            ]);
            $finalData[] = [
                'msg_title' => '系统消息',
                'unread_num' => $sysMsgNum,
                'newlest_msg_title' => '【'.$newSysMsg->title.'】',
                'newlest_msg_time' =>  User::secToTime(time()-strtotime($newlestTG->ctime)).'前'
            ];
        }else{
            $finalData[] = [
                'msg_title' => '系统消息',
                'unread_num' => 0,
                'newlest_msg_title' => '',
                'newlest_msg_time' =>  '',
            ];
        }
        return Json::view(0,'ok',$finalData);
    }



}
